package com.zmn.plat.admin.controller.contract.borrow.record;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.plat.common.constant.CompanyConsts;
import com.zmn.plat.model.entity.company.CompanyTypeQuery;
import com.zmn.plat.model.entity.contract.borrow.record.ContractBorrowRecord;
import com.zmn.plat.model.entity.contract.borrow.record.ContractBorrowRecordQuery;
import com.zmn.plat.model.vo.contract.borrow.ContractBorrowRecordVO;

/**
 * 合同借阅记录表
 *
 * @author linfeng
 * @since 2020-05-09 11:41:45
 */
@Controller
@RequestMapping("/contract/borrow/record/")
public class ContractBorrowRecordListController extends ContractBorrowRecordBaseController {

    /**
     * 列表
     *
     * @param query
     * @author linfeng
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping("list")
    @RequiresPermissions("/contract/borrow/record/list.action")
    public ModelAndView list(@ModelAttribute ContractBorrowRecordQuery query) {

        List<ContractBorrowRecord> items = contractBorrowRecordService.listPageByQuery(query);
        List<Integer> borrowerStaffIds = items.stream().map(e -> e.getBorrower()).collect(Collectors.toList());
        List<StaffDRO> borrowerStaffs = new ArrayList<>();
        if (CollectionUtil.isNotNullOrEmpty(borrowerStaffIds)) {
            ResponseDTO<List<StaffDRO>> data = staffListRemoteService.listStaffByStaffIds(borrowerStaffIds, GlobalConsts.YES);
            if (data.isSuccess()) {
                borrowerStaffs = data.getData();
            }
        }
        Map<Integer, StaffDRO> borrowerStaffMap = borrowerStaffs.stream().collect(Collectors.toMap(e -> e.getStaffId(), e -> e));

        ModelAndView mav = new ModelAndView("contract/borrow/record/list");
        mav.addObject("items", items);
        mav.addObject("borrowerStaffMap", borrowerStaffMap);
        mav.addObject("obj", query);
        mav.addObject("vo", query);
        mav.addObject("flag", 3);
        return mav;
    }

    /**
     * 详情
     *
     * @param contractId
     * @author linfeng
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping("detail")
    @RequiresPermissions("/contract/borrow/record/detail.action")
    public ModelAndView detail(@RequestParam(value = "contractId", defaultValue = "0") Integer contractId,
        @RequestParam(value = "file", defaultValue = "0") String file,
        @RequestParam(value = "fileSrc", defaultValue = "0") String fileSrc) {
        ContractBorrowRecord contractBorrowRecord = new ContractBorrowRecord();

        contractBorrowRecord.setBorrowFile(file);
        contractBorrowRecord.setContractId(contractId);

        List<CompanyTypeQuery> companyTypeList =
            Arrays.asList(new CompanyTypeQuery(CompanyConsts.COMPANY_TYPE_SUBSIDIARY, CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT),
                new CompanyTypeQuery(CompanyConsts.COMPANY_TYPE_HEADOFFICE, CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT));
        List<DictModel> companys = baseCompanyService.listByTypes(companyTypeList, null);
        ContractBorrowRecordVO contractBorrowRecordVO = new ContractBorrowRecordVO();
        BeanUtils.copyProperties(contractBorrowRecord, contractBorrowRecordVO);

        StaffDRO borrowStaff = null;
        ResponseDTO<StaffDRO> data = staffListRemoteService.getStaff(contractBorrowRecordVO.getBorrower());
        if (data.isSuccess()) {
            borrowStaff = data.getData();
        }
        contractBorrowRecordVO.setBorrowStaff(borrowStaff);

        ModelAndView mav = new ModelAndView("contract/borrow/record/detail");
        mav.addObject("contractBorrowRecordVO", contractBorrowRecordVO);
        mav.addObject("companys", companys);
        mav.addObject("oneDepts", getOneDeptList());
        mav.addObject("twoDepts", getTwoDeptList());
        mav.addObject("contractBorrowRecord", contractBorrowRecordVO);
        mav.addObject("fileSrc", fileSrc);
        mav.addObject("flag", 3);
        return mav;
    }

}
